package com.yhcnet.controller;


import com.yhcnet.po.Rename;
import com.yhcnet.po.Result;
import com.yhcnet.service.FileService;
import com.yhcnet.service.UserService;
import com.yhcnet.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileController {

    private int FILES_MAX = 104857600;

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;


    /**
     * 根据用户的 uuid 找到对应的 uid 再找到对应的 files ，不会返回 400 和 false
     * @param uuid
     * @return
     */
    @RequestMapping("/files")
    public Result getFiles(@RequestParam("uuid") String uuid,@RequestParam("pid") int pid, @RequestParam("page") int page,@RequestParam("type") int type) {

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }
        return Result.succ(fileService.getFiles(uuid,pid,page,type));
    }

    @RequestMapping("/search")
    public Result getSearch(@RequestParam("uuid") String uuid,@RequestParam("pid") int pid, @RequestParam("page") int page,@RequestParam("type") int type,@RequestParam("search") String search) {

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        return Result.succ(fileService.getSearch(uuid,pid,page,type,search));
    }

    /**
     * 上传文件  会返回 400 和 false
     * @param file
     * @param uuid
     * @param pid
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("user") String uuid, @RequestParam("pid") Integer pid) throws IOException {
        if(file.isEmpty()){
            return Result.fail("上传失败，请重试！");
        }

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        if (FILES_MAX < fileService.getSizes(uuid) + file.getSize()) {
            return Result.fail("上传文件已超过最大容量 100 M ，请重试！");
        }

        if (fileService.checkExsits(file,uuid,pid)){
            return Result.fail("文件上传重复，请重试！");
        }

        // 特殊，上传成功没有接受返回值的地方，所以返回 null
        if (fileService.upload(file,uuid,pid)) {return Result.succ(null);}

        return Result.fail("上传失败，请重试！");
    }

    @RequestMapping("/create")
    public Result create(@RequestParam("name") String name,@RequestParam("uuid") String uuid, @RequestParam("pid") Integer pid) {
        if (!Utils.isValidFileName(name)) {Result.fail("文件名不合法！");}

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        if(fileService.checkFolder(name,uuid,pid)){
            return Result.fail("已存在重复名的文件夹！");
        }

        if (fileService.create(name,uuid,pid)) {return Result.succ(null);}

        return Result.fail("创建失败，请重试！");
    }

    @RequestMapping("/download")
    public Result download(HttpServletResponse response, @RequestParam("fid") int fid, @RequestParam("uuid") String uuid) throws IOException {
        if(!userService.checkUser(uuid) || fid < 0) {
            return Result.fail("登录过期，请重新登录！");
        }

        fileService.download(response,fid,uuid);

        return Result.succ(null);
    }

    @PostMapping("/rename")
    public Result rename(@RequestBody Rename rename){
        if(rename == null) {return Result.fail("重命名失败，请重试！");}

        String oldName = rename.getOldName();
        String suffix = rename.getSuffix();
        int fid = rename.getFid();
        String uuid = rename.getUuid();

        if(!userService.checkUser(uuid) || fid < 0) {
            return Result.fail("登录过期，请重新登录！");
        }

        if (!Utils.isValidFileName(oldName)) {Result.fail("文件名不合法！");}

        if (fileService.rename(oldName, suffix, fid, uuid)) {return Result.succ(null);}

        return Result.fail("重命名失败，请重试！");
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam("uuid") String uuid,@RequestParam("fid") int fid) {
        if(!userService.checkUser(uuid) || fid < 0) {
            return Result.fail("登录过期，请重新登录！");
        }

        if (fileService.delete(uuid,fid)) {return Result.succ(null);}

        return Result.fail("删除失败，请重试！");
    }

    @RequestMapping("/sizes")
    public Result getSizes(@RequestParam("uuid") String uuid) {
        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        return Result.succ(fileService.getSizes(uuid));
    }

    @RequestMapping("/count")
    public Result getCount(@RequestParam("uuid") String uuid,@RequestParam("pid") int pid ,@RequestParam("type") int type) {
        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        return Result.succ(fileService.getCount(uuid,pid,type));
    }

    @RequestMapping("filesTree")
    public Result getTree(@RequestParam("uuid") String uuid,@RequestParam("pid") int pid) {
        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        return Result.succ(fileService.getTree(uuid,pid));
    }

    // 移动功能，待实现
    @RequestMapping("/moveFiles")
    public Result moveFiles(@RequestParam("uuid") String uuid,@RequestParam("fid") int fid,@RequestParam("tid") int tid){
        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        if(fileService.moveFiles(uuid,fid,tid)){
            return Result.succ(null);
        }

        return Result.fail("移动失败，请重试！");
    }

}
